//
//  CZEmoticonCollectionView.swift
//  WeiBo
//
//  Created by Apple on 16/9/8.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
//  重用标记
 let CZEmoticonCollectionViewCellIdentifier = "CZEmoticonCollectionViewCellIdentifier"

//  表情视图
class CZEmoticonCollectionView: UICollectionView {
    
    //  滚动执行的闭包
    var scrollClosure: ((IndexPath)->())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        super.init(frame: frame, collectionViewLayout: flowLayout)
        //  每项之间的水平间距
        flowLayout.minimumInteritemSpacing = 0
        //  每项之间的垂直间距
        flowLayout.minimumLineSpacing = 0
        //  设置item大小
        flowLayout.itemSize = CGSize(width: ScreenWidth, height: EmoticonCollectionViewHeight)
        
        //  设置滚动放心
        flowLayout.scrollDirection = .horizontal
        //  设置分页
        isPagingEnabled = true
        //  关闭弹簧效果
        bounces = false
        //  滚动指示器
        showsVerticalScrollIndicator = false
        showsHorizontalScrollIndicator = false
        
        
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private func setupUI() {
        
        //  设置数据源代理
        dataSource = self
        //  设置代理
        delegate = self
        
        //  注册cell
        register(CZEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: CZEmoticonCollectionViewCellIdentifier)
    }
    
    //  设置子控件的大小, itemSize会存在一个问题，就是设置itemsize会重写加载数据，导致卡顿
//    override func layoutSubviews() {
//        super.layoutSubviews()
//        print(CACurrentMediaTime())
////        flowLayout.itemSize = size
//    
//    }
    

}

// MARK:    -- 实现UICollectionViewDataSource代理
extension CZEmoticonCollectionView: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return CZEmoticonTools.shareTools.allEmoticonArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return CZEmoticonTools.shareTools.allEmoticonArray[section].count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CZEmoticonCollectionViewCellIdentifier, for: indexPath) as! CZEmoticonCollectionViewCell
        cell.indexPath = indexPath
        cell.emoticonsArray = CZEmoticonTools.shareTools.allEmoticonArray[indexPath.section][indexPath.item]
        
        cell.refreshRecentEmoticon = { [weak self] in
            let recentIndexPath = IndexPath(item: 0, section: 0)
            self?.reloadItems(at: [recentIndexPath])
            
        }
        return cell
    }
    
    //  cell滚动
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("哈哈")
        
        //  判断当前屏幕上那个cell显示的多，获取屏幕上显示的cell
        //  这样获取cell是无序的，需要对其进行x坐标进行排序
        let cells = visibleCells.sorted { (firstCell, secondCell) -> Bool in
            return firstCell.x < secondCell.x
        }
        
        //  x坐标得偏移量
        let contentOffSetX = scrollView.contentOffset.x
        if cells.count == 2 {
            //  判断那个cell显示的多
            
            let firstCell = cells.first!
            let secondCell = cells.last!
            //  第一个cell偏移量差值
            let firstContentOffSetX = abs(firstCell.x - contentOffSetX)
            //  第二个cell偏移量差值
            let secondContentOffSetX = secondCell.x - contentOffSetX
            
            let indexPath: IndexPath
            if firstContentOffSetX < secondContentOffSetX {
                //  第一个cell显示的多
                indexPath = self.indexPath(for: firstCell)!
                
                
                print("第一个cell显示的多")
            } else {
                //  第二个cell显示的多
                indexPath = self.indexPath(for: secondCell)!
                print("第二个cell显示的多")
            }
            
            //  根据indexpath选中指定的toolbar按钮
            //  执行滚动的闭包
            scrollClosure?(indexPath)
            
        }
        
        
        
        
    }
    
}







